package com.hbyj.struts.action;


import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.dialect.DB2390Dialect;

import com.googlecode.jsonplugin.JSONException;
import com.googlecode.jsonplugin.JSONUtil;
import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.YjdcYjdc01;
import com.hbyj.hbm.vo.YjdcYjdc06;
import com.hbyj.hbm.vo.YjsjYjsj01;
import com.opensymphony.xwork2.ActionSupport;


public class YjdcBGByPBAction extends ActionSupport{
	
	//对象
	private YjdcYjdc06 yjdc06;
	
	private File wordFile;

	public File getWordFile() {
		return wordFile;
	}

	public void setWordFile(File wordFile) {
		this.wordFile = wordFile;
	}

	public YjdcYjdc06 getYjdc06() {
		return yjdc06;
	}

	public void setYjdc06(YjdcYjdc06 yjdc06) {
		this.yjdc06 = yjdc06;
	}
	
	public void insertYjdcBGByPb() throws ParseException, UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String fileName = request.getParameter("fileName");
		Object jsoncallback = request.getParameter("jsoncallback");
		String tybh = fileName.split("\\.")[0].split("-")[0];
		String zhdBiaoshi = fileName.split("\\.")[0].split("-")[1];
		String time = fileName.split("\\.")[0].split("-")[2];
		String leixing = fileName.split("\\.")[0].split("-")[3];
		yjdc06.setYjdc06000(tybh);
		yjdc06.setJbqk0102(zhdBiaoshi);
		//yjdc06.setYjdc06010(leixing);
		String timeStr = time.substring(0,4)+"-"+time.substring(4,6)+"-"+time.substring(6,8)+" "+time.substring(8,10)+":"
								+time.substring(10,12)+":"+time.substring(12,14);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date yjdc06080 =  (Date) format.parse(timeStr);
		yjdc06.setYjdc06080(yjdc06080);
		yjdc06.setYjdc06070(fileName);
		boolean flag = true;
		if(leixing.equals("00")){//应急调查报告YD
			String newCode = queryBiggestForYjdcYjdc06ByString("YD");
			yjdc06.setYjdc06010(newCode);
			//将应急调查事件终结
			String hql = "from YjsjYjsj01 where yjsj01010 > '"+timeStr+"' and yjsj01020 ='"+tybh+"' and yjsjjbqk0102 = '"+zhdBiaoshi+"' and yjsj01050 = '01'";
			List<YjsjYjsj01> list = DbTools.findByHql(hql);
			if(list.size()>0){
				for(int i = 0;i<list.size();i++){
					YjsjYjsj01 yjsj01 = list.get(i);
					yjsj01.setYjsj01050("02");
					if(DbTools.updateObj(yjsj01)){
						flag = true;
					}else{
						flag = false;
						break;
					}
				}
			}
		}
		if(leixing.equals("03")){//简报DJ
			String newCode = queryBiggestForYjdcYjdc06ByString("DJ");
			yjdc06.setYjdc06010(newCode);
		}
		//保存到数据库
		if(DbTools.saveObj(yjdc06)&&flag){
			JSONObject jsonObj = JSONObject.fromObject(yjdc06);
			String str = jsoncallback+"('"+jsonObj.toString()+"')";
			ServletActionContext.getResponse().getOutputStream().write(str.getBytes("utf-8"));
		}else{
			String str = jsoncallback+"('false')";
			ServletActionContext.getResponse().getOutputStream().write(str.getBytes("utf-8"));
		}
	}
	
	
	public String queryBiggestForYjdcYjdc06ByString(String str){
		String hql = "from YjdcYjdc06 where yjdc06010 like '"+str+"____' order by yjdc06010 desc";
		String newCode = "";
		 List list = DbTools.findByHql(hql);
		 if(list.size()>0){
			 YjdcYjdc06 yjdc06Biggest = (YjdcYjdc06)list.get(list.size()-1); 
			 String biggest = yjdc06Biggest.getYjdc06010();
			 String num = biggest.split(str)[1];
			 int newCodeNum = Integer.valueOf(num)+1;
			 if(newCodeNum<10){
				 newCode = "000"+newCodeNum;
			 }else if(newCodeNum>=10&&newCodeNum<100){
				 newCode = "00"+newCodeNum;
			 }else if(newCodeNum>=100&&newCodeNum<1000){
				 newCode = "0"+newCodeNum;
			 }else{
				 newCode = newCodeNum+"";
			 }
		 }else{
			newCode = "0001";
		 }
		 return str+newCode;
	}
	
	public void insertYjdcBGFileByPb(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String jsonStr = request.getParameter("id");
		JSONObject obj = JSONObject.fromObject(jsonStr);
		yjdc06 = (YjdcYjdc06)JSONObject.toBean(obj,YjdcYjdc06.class);
		//保存word
		String serverPath="uploadfolder/simple/yjdc/document";
		String getFilePath = ServletActionContext.getServletContext().getRealPath("/"+serverPath);
        String newfilename = yjdc06.getYjdc06070();
        //要存放的文件对象
        File uploadFile  = new File(getFilePath+"/"+newfilename);
        // 复制file对象，实现上传 
        try {
			FileUtils.copyFile(wordFile,uploadFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	//本机的快报导入
	public void insertKb() throws ParseException, UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String fileName = request.getParameter("fileName");
		String tybh = fileName.split("\\.")[0].split("-")[0];
		String zhdBiaoshi = fileName.split("\\.")[0].split("-")[1];
		String time = fileName.split("\\.")[0].split("-")[2];
		String leixing = fileName.split("\\.")[0].split("-")[3];
		yjdc06.setYjdc06000(tybh);
		yjdc06.setJbqk0102(zhdBiaoshi);
		//yjdc06.setYjdc06010(leixing);
		String newCode = queryBiggestForYjdcYjdc06ByString("YK");
		yjdc06.setYjdc06010(newCode);
		String timeStr = time.substring(0,4)+"-"+time.substring(4,6)+"-"+time.substring(6,8)+" "+time.substring(8,10)+":"
								+time.substring(10,12)+":"+time.substring(12,14);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date yjdc06080 =  (Date) format.parse(timeStr);
		yjdc06.setYjdc06080(yjdc06080);
		yjdc06.setYjdc06070(fileName);
		String serverPath="uploadfolder/simple/yjdc/document";
		String getFilePath = ServletActionContext.getServletContext().getRealPath("/"+serverPath);
        String newfilename = yjdc06.getYjdc06070();
        boolean flag = true;
        //要存放的文件对象
        File uploadFile  = new File(getFilePath+"/"+newfilename);
        // 复制file对象，实现上传 
        try {
			FileUtils.copyFile(wordFile,uploadFile);
		} catch (IOException e) {
			flag = false;
		}
		if(DbTools.saveObj(yjdc06)&&flag){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
		
	}
	
	
}